package service

import (
	"api_interface/dao"
	"api_interface/entity"

	"github.com/jinzhu/gorm"
)

//回复评论（测试完成）

func CreateReply(reply *entity.CommentReplyInfo) (err error) {
	tx := dao.SqlSession.Begin()
	if err = tx.Create(reply).Error; err != nil {
		tx.Rollback()
		return err
	}
	if err = tx.Model(&entity.CommentInfo{}).Where("id=?", reply.Cid).Update("comments", gorm.Expr("comments + 1")).Error; err != nil {
		tx.Rollback()
		return err
	}
	return tx.Commit().Error
}

//获取回复（测试完成）

func GetReply(cid int) (reply *[]entity.CommentReplyInfo, err error) {
	var replylist []entity.CommentReplyInfo
	if err = dao.SqlSession.Where("cid=?", cid).Find(&replylist).Error; err != nil {
		return nil, err
	}
	reply = &replylist
	return
}
